﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MyVocabulary.Popup;

namespace MyVocabulary
{
    public partial class VerbTestFilter : Form
    {
        #region Consts

        protected static class FormMessages
        {
            public const string NoConjugation = "Test verbs with no conjugations";
            public const string TestTheFollowingConjugations = "Test the following conjugations:";

            public const string ErrorMessageTitle = "Error";
            public const string SelectOne = "Check at least one element in otder to start the test.";
        }

        #endregion

        public delegate void OnTestFiltersSet(object sender, bool TestVerbsWithNoConjugation, List<string> VerbConjugations);

        public event OnTestFiltersSet OnFilterSet;

        protected List<string> verbConjugations;

        public VerbTestFilter(List<string> verbConjugations, OnTestFiltersSet OnFilterSet)
        {
            InitializeComponent();
            this.verbConjugations = verbConjugations;
            this.OnFilterSet = OnFilterSet;
            CreateCheckBoxes();            
        }

        protected void CreateCheckBoxes()
        {
            Height = verbConjugations.Count * 18 + 135;
            var index = 1;
            pnlCheckboxes.Controls.Clear();

            var cb = new CheckBox
                         {
                             AutoSize = true,
                             Location = new System.Drawing.Point(18, 10),
                             Name = string.Format("cb_{0}", index),
                             Size = new System.Drawing.Size(80, 17),
                             TabIndex = index,
                             Text = FormMessages.NoConjugation,
                             UseVisualStyleBackColor = true,
                             Checked = true
                         };
            pnlCheckboxes.Controls.Add(cb);

            if (verbConjugations.Count == 0)
                return;

            pnlCheckboxes.Controls.Add(new Label
                                           {
                                               AutoSize = true,
                                               Location = new System.Drawing.Point(15, 33),
                                               Text = FormMessages.TestTheFollowingConjugations,
                                               ForeColor = System.Drawing.Color.DarkSlateGray
                                           });

            foreach (var item in verbConjugations)
            {
                index++;
                cb = new CheckBox
                {
                    AutoSize = true,
                    Location = new System.Drawing.Point(35, index * 18 + 18),
                    Name = string.Format("cb_{0}", index),
                    Size = new System.Drawing.Size(80, 17),
                    TabIndex = index,
                    Text = item,
                    UseVisualStyleBackColor = true,
                    Checked = true
                };
                pnlCheckboxes.Controls.Add(cb);                
            }
        }

        #region Buttons

        private void btnOK_Click(object sender, EventArgs e)
        {
            var noConjugation = ((CheckBox) pnlCheckboxes.Controls["cb_1"]).Checked;
            var list = new List<string>();
            for (int i = 0, len = verbConjugations.Count; i < len; i++ )
            {
                var cb = ((CheckBox) pnlCheckboxes.Controls[string.Format("cb_{0}", (i + 2))]);
                if(cb != null && cb.Checked)
                    list.Add(cb.Text);
            }

            if(!noConjugation && list.Count == 0)
            {
                (new CustomMessageBox(FormMessages.SelectOne, FormMessages.ErrorMessageTitle, CustomMessageBoxButtons.OK))
                    .ShowDialog(this);
                return;
            }

            if (OnFilterSet != null)
                OnFilterSet(this, noConjugation, list);
            Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
